﻿using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using SpectrumAnalyser.Common.ServiceHeaders;

namespace SpectrumAnalyser.Services.Common.Inspectors
{
    public class InstanceNameClientMessageInspector : IClientMessageInspector
    {
        private readonly MessageHeader<InstanceNameHeader> _instanceNameMessageHeader;

        public InstanceNameClientMessageInspector(InstanceNameHeader instanceName)
        {
            var instanceNameHeader = instanceName;
            this._instanceNameMessageHeader = new MessageHeader<InstanceNameHeader>(instanceNameHeader);
        }

        #region IClientMessageInspector Members

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            MessageHeader messageHeader = this._instanceNameMessageHeader.GetUntypedHeader(InstanceNameHeader.NAME,
                                                                                           InstanceNameHeader.NAMESPACE);
            request.Headers.Add(messageHeader);
            return null;
        }

        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
        }

        #endregion
    }
}